home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 July: Mac OS SDK / Dev.CD Jul 96 SDK / Dev.CD Jul 96 SDK2.toast / Development Kits (Disc 2) / QuickDraw GX / Programming Stuff / Sample Code / Printing Samples / Printer Drivers… / CustomWriterGX / GlobalData.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-10-05  |  4.5 KB  |  143 lines  |  [TEXT/MPS ]

  1. /*
  2.     FILENAME
  3.         GlobalData.c
  4.  
  5.     DESCRIPTION
  6.         Contains code that shows a prefered way to manage unique global
  7.         data for different message handler instances.  This method
  8.         stores all globals off of the message manager instance context.
  9.         It has the advantage that it will work under any compiler.
  10.         This method is documented in detail in "Inside Macintosh: GX
  11.         Environment & Utilities" starting on page 6-10
  12.         
  13.     COPYRIGHT
  14.         Copyright © 1995 Apple Computer, Inc.
  15.         All rights reserved.
  16.     
  17.     Modification history
  18.         10/04/95 - David Hayward -    Version 1.0.4 modified code so that
  19.                                     the driver can be build under MPW,
  20.                                     Metrowerks, and Symantec.  In general,
  21.                                     all that was required to do this was 
  22.                                     to add an inline-assembly jumptable
  23.                                     and to store all globals off of the
  24.                                     message manager instance context.
  25.                                     Also made a few changes so that the
  26.                                     driver can be rebuilt to support any
  27.                                     resolution by changing the #defines
  28.                                     kResolution and kPatStretch in
  29.                                     "CommonDefines.h"
  30.  
  31.         06/14/95 - Dave Hersey -    Version 1.0.3 to fix a bug in
  32.                                     CustomBufferingAndIO.c when creating
  33.                                     high-res PICTs, and to make the size
  34.                                     of buffers more flexible.
  35.  
  36.         05/26/95 - Dave Hersey -    Version 1.0.2 to add the new 'outp'
  37.                                     desktop printer resource in NewApp.c.
  38.  
  39.         05/03/95 - Dave Hersey -    Version 1.0.1 to fix some minor bugs in
  40.                                     CustomBufferingAndIO.c.
  41.  
  42.         01/14/95 - Dave Hersey -    Begat.
  43. */
  44.  
  45.  
  46. #include "GlobalData.h"
  47.  
  48.  
  49. /*    -----------------------------------------------------------------------
  50.  
  51.     GetGlobals returns the DriverGlobals structure
  52.     whose handle is stored in the MessageHandler InstanceContext.
  53.  
  54.     -----------------------------------------------------------------------    */
  55.  
  56. DriverGlobalsHdl GetGlobals ( void )
  57. {
  58.     DriverGlobalsHdl dataHandle;
  59.     
  60.     dataHandle = (DriverGlobalsHdl) GetMessageHandlerInstanceContext();
  61.  
  62.     return dataHandle;
  63. }
  64.  
  65.  
  66. /*    -----------------------------------------------------------------------
  67.  
  68.     CreateAndStoreGlobals is a routine we call to initialize and store a
  69.     handle in the are we allocated at the end of our NewApp.a jump table.
  70.     
  71.     This routine and its counterpart (DisposeGlobals) provide an alternate
  72.     method of handling global data in a driver.  The two calls provide a
  73.     method similar to GetMessageHandlerInstanceContext and
  74.     SetMessageHandlerInstanceContext, except that this method can be used
  75.     even if your code didn't receive control via a message override.  For
  76.     example, if your code is being executed from a dialog manager
  77.     callback.  The standard global data handling routines that are provided
  78.     with QuickDraw GX will only work if your code received control via a
  79.     message override.
  80.  
  81.     -----------------------------------------------------------------------    */
  82.  
  83. OSErr CreateGlobals ( void )
  84. {
  85.     OSErr                err;
  86.     DriverGlobalsHdl    dataHandle;
  87.     DriverGlobals        defaultGlobals = {    0x12345678,
  88.                                             0,            // lineFeeds - accumulated lines feeds
  89.                                             0,            // pagesImaged - Number of pages imaged.
  90.                                             0,            // lastYPos - Used by our RasterDatIn override.
  91.                                             0,            /// curFileRefNum - refNum of current open file.
  92.                                             0,            // pixMapRowBytes - rowBytes of the pixmap we create.
  93.                                             {0,0,0,0},    // pixMapBounds - Bounds of the pixmap we create.
  94.                                             0,            // hRes - Horizontal output resolution.
  95.                                             0,            // vRes - Vertical output resolution.
  96.                                             {0,0,"\p"},    // fileLocation - Where to put our files.
  97.                                             nil            // buffer - 
  98.                                           } ;
  99.     /*
  100.     Create a new temporary memory handle, initialize
  101.     it, and store it as the message handler's instance
  102.     context.
  103.     */
  104.     
  105.     dataHandle = (DriverGlobalsHdl) TempNewHandle(sizeof(DriverGlobals),&err);
  106.     
  107.     if (err == noErr)
  108.     {
  109.         (**dataHandle) = defaultGlobals ;
  110.         SetMessageHandlerInstanceContext(dataHandle);
  111.     }
  112.     
  113.     return err;
  114. }
  115.  
  116.  
  117. /*    -----------------------------------------------------------------------
  118.  
  119.     DisposeGlobals is a routine that disposes of the global data we stored
  120.     in our jump table's data area via CreateAndStoreGlobals.
  121.  
  122.     -----------------------------------------------------------------------    */
  123.  
  124. void DisposeGlobals ( void )
  125. {
  126.     DriverGlobalsHdl dataHandle;
  127.     
  128.     /*
  129.     Retrieve the message handler's instance context. If the
  130.     value returned isn't nil, it's a handle that we stored
  131.     earlier. Dispose of the handle and set the instance
  132.     context to nil to "clear" it.
  133.     */
  134.     
  135.     dataHandle = (DriverGlobalsHdl) GetMessageHandlerInstanceContext();
  136.     
  137.     if (dataHandle != nil)
  138.     {
  139.         DisposeHandle( (Handle)dataHandle );
  140.         SetMessageHandlerInstanceContext(nil);
  141.     }
  142. }
  143.